package com.ambow.dao;

import java.math.BigInteger;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.ambow.Entity.Operator;
import com.ambow.utils.BaseDAO;
import com.ambow.utils.HibernateUtil;




public class Operat {
	
	@Test
//	public void yingshe(){
//		Configuration conf = new Configuration().configure();
//		SessionFactory sf=conf.buildSessionFactory();
//	}
	
    /**
     * 
     * @param new_account
     * @param new_name
     * @param new_psw
     * @param new_position
     * @param tel
     * @param new_role
     * @param new_remark
     * @return
     * 添加用户
     */
	public boolean addUser(String new_account, String new_name, String new_psw,
			String new_position, String tel, String new_role,String new_remark ){
       
	    Configuration conf = new Configuration().configure();
		SessionFactory sf=conf.buildSessionFactory();
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		
		Operator op = new Operator();
		
		op.setAccount(new_account);
		op.setName(new_name);
		op.setPassword(new_psw);
		op.setPosition(new_position);
		op.setTel(tel);
		op.setRole(new_role);
		op.setRemark(new_remark);
		
		session.save(op);
		session.persist(op);
		
		tx.commit();
		session.close();
		sf.close();
		
		return true;
		    
	}
	/**
	 * 
	 * @param new_psw
	 * @param id
	 *根据id修改密码
	 */

	public void updatePerson(String new_psw,int id){
		try{

//			String new_tel="14785236941";//要修改的值
		    HibernateUtil hibernateUtil = new HibernateUtil();
			Session session = hibernateUtil.getSession();
			session.beginTransaction();
			Query query = session.createQuery("update Operator o set o.password='"+new_psw+"'  where o.id="+id+"");
			query.executeUpdate();
			session.getTransaction().commit();

		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
    /**
     * 
     * @param account
     * @param psw
     * @return
     * 登录查询，返回一个保存了用户所有信息的list
     */
	public List loginSelect1(String account, String psw){
		List list = null;

		try {
			HibernateUtil hibernateUtil = new HibernateUtil();
		    Session session = hibernateUtil.getSession();
			session.beginTransaction();
			Query query = session.createQuery("from Operator o where o.account='"+account+"' and o.password='"+psw+"'");
			list=query.list();
			if(list!=null){
				return list;
			}
			
		 }
			    catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 
	 * @param id
	 * @param postion
	 * @param remark
	 * @param tel
	 * @return
	 * 根据id更新用户信息
	 */

	public boolean updateinfo(int id, String postion, String remark, String tel){
		boolean f=false;
		try {
//		    int id=1001;
//		    String postion="老师";
//		    String remark="你好";
	        HibernateUtil hibernateUtil = new HibernateUtil();
		    Session session = hibernateUtil.getSession();
		    session.beginTransaction();
		    Query query = session.createQuery("update Operator o set o.position='"+postion +"', o.remark='"+remark+"',o.tel='"+tel+"'  where o.id="+id+"");
		    query.executeUpdate();
		    session.getTransaction().commit();
		    
		    f=true;
		 }
	    catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
		return f;
	}
	/**
	 * 
	 * @param role_query
	 * @return
	 * 根据角色查询，返回list
	 */

	public List roleQuery(String role_query){
		List list=null;
//		String role="学生管理员";
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.getSession();
//		session.beginTransaction();
		Query query = session.createQuery("from Operator o where o.role='"+role_query+"'");
		list=query.list();
		if(list!=null){
			return list;
		}
		session.getTransaction().commit();
		
		return list;
	}
	
	public boolean deleteOperator(int user_id){
//		String name="文真棋";
//		int user_id=9;
		HibernateUtil hibernateUtil = new HibernateUtil();
		Session session = hibernateUtil.sessionFactory.openSession();
//		Session session = hibernateUtil.getSession();
		session.beginTransaction();
		
		Query query = session.createSQLQuery("delete from Operator where id="+user_id+"");
		query.executeUpdate();
		session.getTransaction().commit();
		session.close();
	    return true;
	
		
	}
	
	/**
	 * 
	 * @param user_id
	 * @return
	 * 根据用户id查找用户信息，返回List
	 */
	public List updateUserInfo(int user_id){
		List list=null;
		
		
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.sessionFactory.openSession();
		session.beginTransaction();
		
		Query query=session.createQuery("from Operator o where o.id="+user_id+"");
		list=query.list();
		if(list!=null){
			
//			Operator op = (Operator) list.get(0);
//			System.out.println(op.getName());
			return list;
		}
		session.beginTransaction().commit();
		return list;
	}
	
	/**
	 * 
	 * @param user_id
	 * @param user_password
	 * @param user_postion
	 * @param user_tel
	 * @param user_remark
	 * @return
	 * 根据id修改信息，返回Boolean值
	 */
	public boolean update_userinfo(int user_id, String user_password, String user_postion, String user_tel,String user_remark ){
//		int user_id=1;
//		String user_password="147";
//		String user_postion="可怜";
//		String user_tel="159951";
//		String user_remark="kanaknkna";
		
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.sessionFactory.openSession();
		
		
		Query query= session.createQuery("update Operator set password='"+user_password+"', position='"+user_postion+"', tel='"+user_tel+"', remark='"+user_remark+"' where id="+user_id+"");
		query.executeUpdate();
		session.beginTransaction().commit();
		session.close();
		return true;
	}
   public Operator getOne(Class c,int id) {
		return (Operator) BaseDAO.get(c, id);
	}
	
	

}
